package com.xxx.rpc.sample.filter;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.xxx.rpc.common.bean.RpcRequest;
import com.xxx.rpc.common.bean.RpcResponse;
import com.xxx.rpc.starter.Filter;
import com.xxx.rpc.starter.RpcClient;

@Component
public class LogFilter implements Filter {

	@Override
	public RpcResponse invoke(RpcClient invoker, RpcRequest invocation) throws RuntimeException {
		RpcResponse result = null;
         
        try{
            //before filter
            System.out.println("参数为："+JSONObject.toJSONString(invocation, true));
            result = invoker.send(invocation);
            if (result.getException() instanceof Exception)
            {
                throw new Exception(result.getException());
            }
            System.out.println("响应为："+JSONObject.toJSONString(result, true));
        }
        catch(Exception e){
            e.printStackTrace();
            result = new RpcResponse(e);
            return result;
        }
 
        return result;
	}

}
